#!/bin/bash
#
# Copyright (c) 2012 - mDroidd (martijn.ruijzendaal@gmail.com)
#
# Please ask permission to use parts of this script
# I will be happy to give you!
#
# Credits go to: tommytomatoe, bash, android, github, DEVs
#

# Attention! Because the source section is device-specific, I made some
# Values here to make it easier for you to edit.
#

	function Source
	{
echo "
Hi there!

I can imagegine some people would like me to include their device into the Source list.
That is possible!

Press ENTER to continue!"
read line
clear
echo
echo "Enter the device brand and codename."
echo -n "Enter option [Example: Samsung Crespo]:  "
read opt
set $opt
Brand=$1
CodeName=$2
clear; echo "Github will be opened. On the first tab opened ($2 device), you need to pick"
echo "a git to grab the device folder from. Koush & CyanogenMod are recommended to use."
echo "Press ENTER"
read line
xdg-open "https://github.com/search?q=$1 device&type=Everything&repo=&langOverride=&start_value=1" > /dev/null

echo "When clicked on one of the links, copy the link FROM ://"
echo "[Example] ://github.com/CyanogenMod/android_device_samsung_crespo.git"
echo "You can paste using Shift+Insert"
echo -n "Enter git:  "
read Device
DeviceGit=git$Device
clear
echo "Good. Now another TAB will be opened ($2 vendor)."
echo "Press enter"
read line
xdg-open "https://github.com/search?q=$1 vendor&type=Everything&repo=&langOverride=&start_value=1" > /dev/null
echo "Do exactly the same as you did before;"
echo "Pick one of the gits (Koush / Cyanogen), copy the link from ://"
echo -n "Paste the link here:  "
read Vendor
VendorGit=git$Vendor
clear
echo "This is what you entered:

$Brand $CodeName
device: $Device
vendor: $Vendor"
echo "git clone $DeviceGit
git clone $VendorGit
$Brand $CodeName" > ~/IncludeSOURCE

echo "PM Me the automatically generated file IncludeSOURCE in your home directory,"
echo "IF you want me to include!"
	
	if ! [ -d $ScriptDir/WORKING_DIR_$1 ]
	then
		mkdir $ScriptDir/WORKING_DIR_$1
		cd $ScriptDir/WORKING_DIR_$1
	else
		cd $ScriptDir/WORKING_DIR_$1
	fi
	if ! [ -d $ScriptDir/WORKING_DIR_$1/.repo ]
	then
	   if [ "$1" = "1" ]
	   then
	    GBSOURCE
	   fi
	   if [ "$1" = "2" ]
	   then
	    ICSSOURCE
	   fi
	   if [ "$1" = "3" ]
	   then
	    CMSOURCE
	   fi
	   if [ "$1" = "4" ]
	   then
	    OxGBSOURCE
	   fi
	   if [ "$1" = "5" ]
	   then
	    AOKPSOURCE
	   fi
	   if [ "$1" = "6" ]
	   then
	    OxICSSOURCE
	   fi
	   if [ "$1" = "7" ]
	   then
	    MiuiSOURCE
	   fi
	   if [ "$1" = "8" ]
	   then
	    CnaSource
	   fi
	fi
	}
	mkdir -p $Dir/device/$Brand
	git clone $GitDevice $Dir/device/$Brand
	mkdir -p $Dir/vendor/$Brand
	git clone $GtiVendor $Dir/vendor/$Brand

	if [ -d $Dir/device/$Brand/*/*liboverlay ]
	then
		rm -rf $SDir/hardware/qcom/display/liboverlay
	fi
	if [ -d $Dir/device/$Brand/*/*libgenlock ]
	then
		rm -rf $SDir/hardware/qcom/display/libgenlock
	fi
	if [ -d $Dir/device/$Brand/*/*libqcomui ]
	then
		rm -rf $SDir/hardware/qcom/display/libqcomui
	fi
	if [ -d $Dir/device/$Brand/*/*omx* ] || [ -d $Dir/device/$Brand/*/*mm* ]
	then
		rm -rf $SDir/hardware/qcom/mm-core
	fi
	Include=$(tail -1 $Dir/device/$Brand/*/vendorsetup.sh)
	echo "$Include" > $(cd $Dir/vendor && find -name vendorsetup.sh)

	ICSSOURCE="ICS Source"

# ^^ Is the value that will be displayed in menu

	function ICSSOURCE
	{
	repo init -u https://android.googlesource.com/platform/manifest -b android-4.0.1_r1
	}

# ^^ That are the commands that will be executed when option is chosen
# Change the "repo init -u" to the one who ported the ROM to your device or stock

	GBSOURCE="Stock AOSP GB Source"

	function GBSOURCE
	{
	repo init -u https://android.googlesource.com/platform/manifest -b android-2.3.7_r1
	repo sync
	}

	CMSOURCE="CyanogenMod7 Source"

	function CMSOURCE
	{
	repo init -u git://github.com/CyanogenMod/android.git -b gingerbread
	repo sync
	}

	OxGBSOURCE="Oxygen Source"

	function OxGBSOURCE
	{
	repo init -u git://github.com/popdog123/android_manifest.git -b gingerbread
	repo sync
	}

	AOKPSOURCE="Android Open Kang SOURCE"

	function AOKPSOURCE
	{
	repo init -u https://github.com/AOKP/platform_manifest.git -b master
	repo sync
	if ! [ -d $SDir/vendor/lge ]
	then
		mkdir $SDir/vendor/lge
		mkdir $SDir/device/lge
	fi
	if ! [ -d $SDir/vendor/lge/* ]
	then
		git clone git://github.com/lupohirp/LGP500-ics-vendor-files.git -b ics vendor/lge
		git clone git://github.com/lupohirp/LGP500-ics-device-folder.git -b aokp device/lge
	fi

	echo "add_lunch_combo thunderg-user" >> $SDir/vendor/aokp/vendorsetup.sh
	rm -rf $SDir/external/webkit
	rm -rf $SDir/hardware/qcom/display/liboverlay
	rm -rf $SDir/hardware/qcom/display/libgenlock
	rm -rf $SDir/hardware/qcom/display/libqcomui
	rm -rf $SDir/hardware/qcom/mm-core/omxcore
	}

	OxICSSOURCE="[UNAVAILABLE]Oxygen ICS Source"

	function OxICSSOURCE
	{
	#repo init -u
	#repo sync
	clear; echo "Not defined yet!"
	}

	CNASOURCE="[NEW] CodeName Android Source (ICS)"
	function CnaSource
	{
	repo init -u git://github.com/CNA/android_manifest.git -b mod-4.0.3
	repo sync
	echo "add_lunch_combo thunderg-user" >> $SDir/vendor/cna/vendorsetup.sh
	rm -rf $SDir/external/webkit
	rm -rf $SDir/hardware/qcom/display/liboverlay
	rm -rf $SDir/hardware/qcom/display/libgenlock
	rm -rf $SDir/hardware/qcom/display/libqcomui
	rm -rf $SDir/hardware/qcom/mm-core/omxcore
	}
